<?php
 Session_Start () ;
 If (!$_SESSION ["repertoire_config"]) { Header ("Location: ../../") ; }
 Include ("../../include/garennes.cfg") ;
 Include ("../../include/init.php") ;
 Include ("../include/config.inc") ;
 verif_droits ($droit_courant, $_SESSION ["code"]) ;
 
 Include ("../../include/var_temp.inc") ;
 
 $requete = var_courante ("requete", 0, Null) ;
 $id_abs_validite = var_courante ("id_abs_validite", 0, 0) ;
 $onglet = var_courante ("onglet", 0, 1) ;
 $message = var_courante ("message", 0, Null) ;
 
 If (IsSet ($requete))
  {
   Switch ($requete)
    {
      Case 1 : // Suppression d'une validite
	       If ($id_abs_validite > 0)
	        {
                 $ok = SQL_commande ("DELETE FROM abs_validites WHERE id_abs_validite='".$id_abs_validite."' LIMIT 1", $id_sql) ;
                 $message = 1 ;
		}
	       Else { $message = -1 ; }
	       Echo "<html><head>" ;
	       Echo "<script type=\"text/javascript\">window.open(\"./abs_parametrage.php?code=".$_SESSION ["code"]."&onglet=".$onglet."&message=".$message."\",\"travail_affichage\",\"\");window.close();</script>" ;
	       Echo "</head></html>" ;
               Exit () ;
      Case 2 : // Modification d'une validite
	       If ($id_abs_validite > 0)
	        {
                 $cd = var_courante ("cd", 0, "") ;
	         $nom_court = var_courante ("nom_court", 0, "") ;
	         $nom_long = var_courante ("nom_long", 0, "") ;
	         $numero = var_courante ("numero", 0, 0) ;
	         $cout = var_courante ("cout", 0, 0) ;
                 $requete = "UPDATE abs_validites SET code='".Str_Replace ("'", "\\'", $cd)."',nom_court='".Str_Replace ("'", "\\'", $nom_court)."'," ;
                 $requete .= "nom_long='".Str_Replace ("'", "\\'", $nom_long)."',numero='".$numero."',cout='".Str_Replace (",", ".", $cout)."'" ;
                 For ($i = 1 ; $i < 53 ; $i++)
                  {
		   $requete .= ",sem" ; If ($i < 10) { $requete .= "0" ; }
                   $requete .= $i ;
		   $s = var_courante ("sem".$i, 0, 0) ;
                   If ($s) { $s = "1" ; } Else { $s = "0" ; }
                   $requete .= "='".$s."'" ;
		  }
		 $requete .= " WHERE id_abs_validite='".$id_abs_validite."' LIMIT 1" ;
                 $ok = SQL_commande ($requete, $id_sql) ;
		 $message = 2 ;
                }
	       Else { $message = -2 ; }
	       Break ;
      Case 3 :
     Default : // Ajout d'une validite
               $cd = var_courante ("cd", 0, "") ;
	       $nom_court = var_courante ("nom_court", 0, "") ;
	       $nom_long = var_courante ("nom_long", 0, "") ;
	       $numero = var_courante ("numero", 0, 0) ;
	       $cout = var_courante ("cout", 0, 0) ;
               $requete = "INSERT INTO abs_validites (id_abs_validite,code,nom_court,nom_long,numero,cout" ;
               For ($i = 1 ; $i < 53 ; $i++)
                {
                 $requete .= ",sem" ; If ($i < 10) { $requete .= "0" ; }
                 $requete .= $i ;
                }
               $requete .= ") VALUES ('','".Str_Replace ("'", "`", $cd)."','".Str_Replace ("\'", "`", $nom_court)."','".Str_Replace ("\'", "`", $nom_long)."','".$numero."','".Str_Replace (",", ".", $cout)."'" ;
               For ($i = 1 ; $i < 53 ; $i++)
                {
		 $s = var_courante ("sem".$i, 0, 0) ;
                 If ($s) { $s = 1 ; } Else { $s = 0 ; }
                 $requete .= ",'".$s."'" ;
                }
               $requete .= ")" ;
               $ok = SQL_commande ($requete, $id_sql) ;
               $message = 3 ;
	       Echo "<html><head>" ;
	       Echo "<script type=\"text/javascript\">window.open(\"./abs_parametrage.php?onglet=".$onglet."&message=".$message."\",\"travail_affichage\",\"\");window.close();</script>" ;
	       Echo "</head></html>" ;
               Exit () ;
      Case 4 : // Rappel page principale
               Echo "<html><head>" ;
	       Echo "<script type=\"text/javascript\">window.open(\"./abs_parametrage.php?code=".$_SESSION ["code"]."&onglet=".$onglet."&message=".$message."\",\"travail_affichage\",\"\");window.close();</script>" ;
	       Echo "</head></html>" ;
               Break ;
    }
  }

 $resultats = SQL_commande ("SELECT * FROM menus WHERE numero='".$droit_courant."' AND ordre='0'", $id_sql) ;
 $l = SQL_tableau_associatif ($resultats, "") ; 

 If ($id_abs_validite > 0)
  {
   $resultat = SQL_commande ("SELECT * FROM abs_validites WHERE id_abs_validite='".$id_abs_validite."' LIMIT 1", $id_sql) ;
   $valide = SQL_tableau_associatif ($resultat, "") ;
  }

 Echo "<html><head>" ;
 entete_html ("Garennes", "", 1) ;
 Echo styles ("none", "none", "none", $l ["lien"], "none") ;
 Include ("./abs_edite_validites.js") ;
 Echo "</head>" ;
 
 Echo "<body background=\"../images/fond.png\" text=\"#000000\">" ;
 //Echo "<script type=\"text/javascript\">top.travail.travail_menu.document.aj_groupe.id_abs_validite.value=0;</script>" ;
 Echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" ;
 Echo "<tr>" ;
 Echo "<td></td>" ;
 Echo "<td width=\"100%\" align=\"center\">" ;
 If ($id_abs_validite == 0) { Echo "Ajouter&nbsp;une&nbsp;nouvelle&nbsp;validit&eacute;" ; }
 Else { Echo "&Eacute;dition&nbsp;de&nbsp;la&nbsp;validit&eacute;&nbsp;<font color=\"#0000FF\"><i>".$nom."</i></font>" ; }
 Echo "</td>" ;
 Echo "<td><a href=\"javascript:ouvrefenetre('./aide.php?aide=5&rep=".$_SESSION ["repertoire_config"]."',350,650);\" oldref><img src=\"../images/aide.png\" border=\"0\"></a>" ;
 Echo "<a href=\"javascript:window.close();\" oldref><img src=\"../../images/quitter.png\" border=\"0\"></a>" ;
 Echo "</td>" ;
 Echo "</tr>" ;
 Echo "<table>" ;
 If ($message)
  {
   Echo "<center><font size=\"".$l ["lien"]."\" color=\"#FF0000\"><b>La&nbsp;validit&eacute;&nbsp;" ;
   If ($message > 0) { Echo "a&nbsp;&eacute;t&eacute;" ; } Else { Echo "n'a&nbsp;pas&nbsp;&eacute;t&eacute;" ; $message = -$message ; }
   Echo "&nbsp;correctement&nbsp;<blink>" ;
   Switch ($message)
    {
     Case 1 : Echo "effac&eacute;e" ; Break ;
     Case 2 : Echo "modifi&eacute;e" ; Break ;
     Case 3 : Echo "ajout&eacute;e" ; Break ;
    }
   Echo "</blink>&nbsp;!</b></font></center>" ;
  }
 Echo "<table border=\"1\" align=\"center\" bgcolor=\"".$l ["couleur_fond"]."\" cellspacing=\"0\" cellpadding=\"0\">" ;
 Echo "<tr><td><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" ;
 
 // Vide
 Echo "<tr>" ;
 Echo "<td colspan=\"2\" align=\"center\">&nbsp;*&nbsp;:&nbsp;Saisie&nbsp;obligatoire&nbsp;</td>" ;
 $ok = SQL_commande ("SELECT id_abs_edt FROM abs_edt WHERE abs_validite='".$id_abs_validite."' LIMIT 1", $id_sql) ;
 If (($id_abs_validite > 0) And (SQL_nbr_lignes ($ok) == 0))
  {
   Echo "<form action=\"".$_SERVER ["PHP_SELF"]."\" name=\"efface\" method=\"post\" OnSubmit=\"return valide ('".Str_Replace ("'", "\\'", $valide ["nom_long"])."')\">" ;
   Echo "<td align=\"center\" rowspan=\"8\" valign=\"top\">" ;
   Echo "<input type=\"hidden\" name=\"code\" value=\"".$_SESSION ["code"]."\">" ;
   Echo "<input type=\"hidden\" name=\"id_abs_validite\" value=\"".$id_abs_validite."\">" ;
   Echo "<input type=\"hidden\" name=\"requete\" value=\"1\">" ;
   Echo "<input type=\"hidden\" name=\"onglet\" value=\"".$onglet."\">" ;
   Echo "<input type=\"image\" src=\"../images/absences/supprimer.png\" border=\"0\">" ;
   Echo "</td>" ;
   Echo "</FORM>" ;
  }
 Else { Echo "<td rowspan=\"8\">&nbsp;</td>" ; }
 Echo "</tr>" ;
 
 // Code
 Echo "<form action=\"".$_SERVER ["PHP_SELF"]."\" name=\"saisie\" method=\"post\" OnSubmit=\"return valide_saisie();\">" ;
 Echo "<input type=\"hidden\" name=\"id_abs_validite\" value=\"".$id_abs_validite."\">" ;
 Echo "<input type=\"hidden\" name=\"requete\" value=\"" ;
 If ($id_abs_validite > 0) { Echo "2" ; } Else { Echo "3" ; }
 Echo "\">" ;
 Echo "<tr>" ;
 Echo "<td align=\"right\"><font size=\"".$l ["titre"]."\">&nbsp;*<b>Code</b>&nbsp;:&nbsp;</FONT></td>" ;
 Echo "<td><font size=\"".$l ["titre"]."\"><input type=\"text\" name=\"cd\" value=\"".$valide ["code"]."\" SIZE=\"6\" MAXLENGTH=\"5\"></FONT>&nbsp;</td>" ;
 Echo "</tr>" ;
 
 // Nom court
 Echo "<tr>" ;
 Echo "<td align=\"right\"><font size=\"".$l ["titre"]."\">&nbsp;*<b>Nom&nbsp;court</b>&nbsp;:&nbsp;</FONT></td>" ;
 Echo "<td><font size=\"".$l ["titre"]."\"><input type=\"text\" name=\"nom_court\" value=\"".$valide ["nom_court"]."\" SIZE=\"15\" MAXLENGTH=\"10\"></FONT>&nbsp;</td>" ;
 Echo "</tr>" ;
 
 // Nom long
 Echo "<tr>" ;
 Echo "<td align=\"right\"><font size=\"".$l ["titre"]."\">&nbsp;<b>Nom&nbsp;long</b>&nbsp;:&nbsp;</FONT></td>" ;
 Echo "<td><font size=\"".$l ["titre"]."\"><input type=\"text\" name=\"nom_long\" value=\"".$valide ["nom_long"]."\" SIZE=\"45\" MAXLENGTH=\"40\"></FONT>&nbsp;</td>" ;
 Echo "</tr>" ;
 
 // Numero
 Echo "<tr>" ;
 Echo "<td align=\"right\"><font size=\"".$l ["titre"]."\">&nbsp;*<b>Num&eacute;ro</b>&nbsp;:&nbsp;</FONT></td>" ;
 Echo "<td><font size=\"".$l ["titre"]."\"><input type=\"text\" name=\"numero\" value=\"".$valide ["numero"]."\" SIZE=\"5\" MAXLENGTH=\"6\"></FONT>&nbsp;</td>" ;
 Echo "</tr>" ;
 
 // Cout
 Echo "<tr>" ;
 Echo "<td align=\"right\"><font size=\"".$l ["titre"]."\">&nbsp;<b>Co&ucirc;t</b>&nbsp;:&nbsp;</FONT></td>" ;
 Echo "<td><font size=\"".$l ["titre"]."\"><input type=\"text\" name=\"cout\" value=\"".Str_Replace (".", ",", $valide ["cout"])."\" SIZE=\"6\" MAXLENGTH=\"5\"></FONT>&nbsp;</td>" ;
 Echo "</tr>" ;
 
 // Semaines
 Echo "<tr>" ;
 Echo "<td align=\"right\" valign=\"top\"><font size=\"".$l ["titre"]."\">&nbsp;<b>Semaines</b>&nbsp;:&nbsp;</FONT></td>" ;
 Echo "<td><table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>" ;
 $c = 1 ;
 For ($i = 1 ; $i < 53 ; $i++)
  {
   $s = "sem" ; If ($i < 10) { $s .= "0" ; }
   $s .= $i ;
   $sem = $valide [$s] ;
   $resultats = SQL_commande ("SELECT ouvrable,date FROM abs_calendrier WHERE semaine='".$i."' ORDER BY date LIMIT 1", $id_sql) ;
   $res = SQL_ligne_suivante ($resultats) ;
   Echo "<td" ;
   If ($res [0] == 1)
    {
     If ($c%2) { Echo " bgcolor=\"".$l ["nom"]."\"" ; }
     Echo "><input type=\"checkbox\" name=\"sem".$i."\"" ;
     If ($sem == 1) { Echo " checked" ; }
     Echo "></td><td" ; If ($c%2) { Echo " bgcolor=\"".$l ["nom"]."\"" ; }
     Echo " title=\"Semaine du " ;
     Echo SubStr ($res [1], 8, 2)." ".$nom_mois [(integer)SubSTr ($res [1], 5, 2)]." ".SubStr ($res [1], 0, 4)."\""  ;
     Echo "\">" ;
    }
   Else
    {
     Echo " bgcolor=\"".$l ["lien"]."\"><input type=\"hidden\" name=\"".$s."\" value=\"0\"></td><td bgcolor=\"".$l ["lien"]."\" title=\"Semaine du " ;
     Echo SubStr ($res [1], 8, 2)." ".$nom_mois [(integer)SubSTr ($res [1], 5, 2)]." ".SubStr ($res [1], 0, 4)."\""  ;
     Echo "\">" ;
    }
   Echo "&nbsp;<b>".SubStr ($res [1], 8, 2)."/".SubStr ($res [1], 5, 2)."/".SubStr ($res [1], 0, 4)."</b>&nbsp;</td>" ;
   If ($i%4 == 0) { $c -= 1 ; }
   If (($i%4 == 0) And ($i < 52)) { Echo "</tr><tr>" ; }
   $c++ ;
  }
 Echo "</tr></table></td>" ;
 Echo "</tr>" ;
 Echo "<tr><td colspan=\"2\">&nbsp;</td></tr>" ;
 Echo "<tr>" ;
 Echo "<td align=\"center\" colspan=\"2\">" ;
 Echo "<input type=\"hidden\" name=\"code\" value=\"".$_SESSION ["code"]."\">" ;
 Echo "<input type=\"hidden\" name=\"id_abs_validite\" value=\"".$id_abs_validite."\">" ;
 Echo "<input type=\"hidden\" name=\"onglet\" value=\"".$onglet."\">" ;
 Echo "<input type=\"image\" src=\"../images/absences/valider.png\" name=\"submit\" value=\"Valider\" border=\"0\">" ;
 Echo "</td>" ;
 Echo "</form>" ;
 Echo "<td align=\"center\">" ;
 Echo "<img src=\"../images/absences/annuler.png\" border=\"0\" onClick=\"javascript:window.open('./abs_parametrage.php?code=".$_SESSION ["code"]."&onglet=".$onglet."&message=".$message."','travail_affichage','');window.close();\">" ;
 Echo "</td>" ;
 Echo "</tr></table>" ;
 Echo "</td></tr></table>" ;
 Echo "</body></html>" ;
?>